package com.sl.hx.shop.ui.collect;

import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sl.hx.shop.R;
import com.sl.hx.shop.base.BaseFragment;
import com.sl.hx.shop.bean.CollectList;
import com.sl.hx.shop.bean.RelationBean;
import com.sl.hx.shop.constant.SPUKey;
import com.sl.hx.shop.ui.collect.adapter.CollectGoodsAdapter;
import com.sl.hx.shop.ui.collect.adapter.RecommendAdapter;
import com.sl.hx.shop.ui.collect.contacts.CollectListContacts;
import com.sl.hx.shop.ui.collect.presenter.CollectListPresenter;
import com.sl.hx.shop.ui.home.activity.GoodsDetailActivity;
import com.sl.hx.shop.ui.home.activity.HotListActivity;
import com.sl.hx.shop.utils.NotifySetting;
import com.sl.hx.shop.utils.SPUtils;
import com.sl.hx.shop.utils.ScreenUtil;
import com.sl.hx.shop.utils.ToastUtil;
import com.sl.hx.shop.widget.DeleteRecyclerView;
import com.sl.hx.shop.widget.OnItemClickListener;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.sl.hx.shop.constant.AppConfig.ID;


public class CollectFragment extends BaseFragment<CollectListContacts.view, CollectListPresenter> implements CollectListContacts.view {

    @BindView(R.id.iv_close)
    ImageView mIvClose;
    @BindView(R.id.collect_recycleview)
    DeleteRecyclerView mCollectRecycle;
    @BindView(R.id.recommend_recycle)
    RecyclerView mRecommendrecycle;
    @BindView(R.id.tv_open)
    TextView tvOpen;
    @BindView(R.id.rl_notice)
    RelativeLayout rlNotice;
    @BindView(R.id.ll_recommend)
    ViewGroup ll_recommend;

    private CollectGoodsAdapter mGoodsAdapter;
    private RecommendAdapter mRecommendAdapter;
    private List<CollectList> datas = new ArrayList<>();
    private List<RelationBean> relationBeans = new ArrayList<>();
    private int page=1;
    private int num=10;

    @Override
    protected CollectListPresenter createPresenter() {
        return new CollectListPresenter(mContext);
    }

    @Override
    protected int getViewLayoutId() {
        return R.layout.fragment_collect;
    }

    @Override
    protected void initView(View mView) {
        initrecycle();
    }

    private void initrecycle() {
        mGoodsAdapter = new CollectGoodsAdapter(datas);
        //空视图
        View emptyView = getLayoutInflater().inflate(R.layout.fragment_collect_empty, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.findViewById(R.id.empty_rootview).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        emptyView.findViewById(R.id.btn_shop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //热销排行页
                Intent intent = new Intent(mActivity, HotListActivity.class);
                startActivity(intent);
            }
        });
        mGoodsAdapter.setEmptyView(emptyView);
        mCollectRecycle.setLayoutManager(new LinearLayoutManager(mActivity));
        mCollectRecycle.setAdapter(mGoodsAdapter);
        mCollectRecycle.setNestedScrollingEnabled(false);//禁止滑动

        mCollectRecycle.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(mActivity, GoodsDetailActivity.class);
                intent.putExtra("user_type", mGoodsAdapter.getItem(position).getUser_type());
                intent.putExtra("num_iid", mGoodsAdapter.getItem(position).getNum_iid());
                startActivity(intent);
            }

            @Override
            public void onDeleteClick(int position) {
                //删除数据
                if (mGoodsAdapter.getItem(position).getNum_iid()!=null) {
                    mPresenter.delCollect(mGoodsAdapter.getItem(position).getNum_iid());
                    mGoodsAdapter.remove(position);
                }
            }
        });

        //推荐列表
        mRecommendAdapter = new RecommendAdapter(relationBeans);
        mRecommendrecycle.setLayoutManager(new GridLayoutManager(mActivity,2));
        mRecommendrecycle.setAdapter(mRecommendAdapter);
        mRecommendrecycle.addItemDecoration(new MarginDecoration(mContext));
        mRecommendAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mActivity, GoodsDetailActivity.class);
                intent.putExtra("user_type",mRecommendAdapter.getItem(position).getUser_type());
                intent.putExtra("num_iid",mRecommendAdapter.getItem(position).getNum_iid());
                startActivity(intent);
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        //请求商品列表
        if (SPUtils.getInstance().getString(ID)!=null&&!TextUtils.isEmpty(SPUtils.getInstance().getString(ID))) {
            mPresenter.getCollectList(page, num);
        }else {
            //id为空显示空数据
            mGoodsAdapter.setNewData(datas);
        }
        //判断消息通知是否开启
        if (isNotificationEnabled()){
            rlNotice.setVisibility(View.GONE);
        }
    }

    @Override
    public void ShowGoodsId(List<CollectList> collectList) {
        mGoodsAdapter.setNewData(collectList);
        //根据第一个id请求相关推荐列表
        mPresenter.getRelation(collectList.get(0).getNum_iid());
    }

    @Override
    public void delCollectSuccess() {
        ToastUtil.showShort(mActivity, "删除成功");
    }

    @Override
    public void ShowRelation(List<RelationBean> relationBeans) {
        //有数据时显示布局
        ll_recommend.setVisibility(View.VISIBLE);
        mRecommendAdapter.setNewData(relationBeans);
    }

    //相关推荐无数据
    @Override
    public void ShowRelationNoData() {
        //无数据时隐藏布局
        ll_recommend.setVisibility(View.GONE);
    }


    @OnClick({R.id.iv_close, R.id.tv_open})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                rlNotice.setVisibility(View.GONE);
                break;
            //跳转设置页开启通知
            case R.id.tv_open:
                NotifySetting.gotoNotifySet(mContext);
                break;
        }
    }

    private boolean isNotificationEnabled() {
        NotificationManagerCompat notification = NotificationManagerCompat.from(getActivity());
        boolean isEnabled = notification.areNotificationsEnabled();
        SPUtils.getInstance().put(SPUKey.ISRECEIVEMESSAGE, isEnabled);
        return  isEnabled;
    }



    //recycleview item设置间距
    class MarginDecoration extends RecyclerView.ItemDecoration {
        private int margintop,marginleft,marginright;

        public MarginDecoration(Context context) {
            margintop = ScreenUtil.dp2px(context,4);
            marginleft = ScreenUtil.dp2px(context,16);
            marginright = ScreenUtil.dp2px(context,4);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            if (parent.getChildLayoutPosition(view) %2 ==0) {
                outRect.set(marginleft,margintop,marginright,margintop);
            }else {
                outRect.set(marginright,margintop,marginleft,margintop);
            }
        }
    }


}
